package server;

import contract.HelloWorldService;
import org.apache.thrift.TException;
import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TJSONProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;

/**
 * @Author: admin
 * @Description:
 * @Date: 2019/3/7 16:13
 */

public class ThriftServer implements HelloWorldService.Iface {
    @Override
    public String sayHello(String username) throws TException {
        System.out.println(username);
        return "hello: "+username;
    }

    public static void main(String[] args) throws TTransportException {
        //simple();
        mult();
    }

    public static void simple() throws TTransportException {
        // 应用层  数据处理业务逻辑
        TProcessor processor=new HelloWorldService.Processor<HelloWorldService.Iface>(new ThriftServer());
        // 传输层
        TServerTransport serverTransport = new TServerSocket(9999);


        TServer.Args tArgs=new TServer.Args(serverTransport);
        tArgs.processor(processor);
        //tArgs.processor(multiplexedProcessor);
        //  数据格式
        tArgs.protocolFactory(new TCompactProtocol.Factory());
        //tArgs.protocolFactory(new TBinaryProtocol.Factory());
        //tArgs.protocolFactory(new TJSONProtocol.Factory());

        TServer server=new TSimpleServer(tArgs);
        server.serve();

    }

    public static void mult() throws TTransportException {
        // 应用层  数据处理业务逻辑
        TProcessor processor=new HelloWorldService.Processor<HelloWorldService.Iface>(new ThriftServer());
        // 传输层
        TServerTransport serverTransport = new TServerSocket(9999);

        TMultiplexedProcessor multiplexedProcessor=new TMultiplexedProcessor();
        multiplexedProcessor.registerProcessor("hello",processor);

        TServer.Args tArgs=new TServer.Args(serverTransport);
        tArgs.processor(multiplexedProcessor);
        //  数据格式
        tArgs.protocolFactory(new TCompactProtocol.Factory());
        //tArgs.protocolFactory(new TBinaryProtocol.Factory());
        //tArgs.protocolFactory(new TJSONProtocol.Factory());

        TServer server=new TSimpleServer(tArgs);
        server.serve();

    }

}
